#ifndef __H__MVP_NODE_READER_H__
#define __H__MVP_NODE_READER_H__

#include <functional>
#include <memory>

#include "transport/receiver/receiver.h"
#include "transport/transport.h"

namespace motovis
{

template <typename MessageT>
using CallbackFunc = std::function<void(const std::shared_ptr<MessageT> &)>;

template <typename MessageT> class Reader
{
  public:
    using ReceiverPtr = std::shared_ptr<transport::Receiver<MessageT>>;

    virtual ~Reader();
    Reader(const std::string &channel_name,
           const CallbackFunc<MessageT> &reader_func,
           const transport::TransportModel &model);

  private:
    std::string channel_name_;
    ReceiverPtr receiver_;
};

template <typename MessageT>
Reader<MessageT>::Reader(const std::string &channel_name,
                         const CallbackFunc<MessageT> &reader_func,
                         const transport::TransportModel &model)
    : channel_name_(channel_name)
{
    receiver_ =
        transport::Transport::Instance()->createReceiver<MessageT>(channel_name_, model);
    receiver_->addListener(channel_name_, reader_func);
}

template <typename MessageT> Reader<MessageT>::~Reader()
{
    receiver_ = nullptr;
}

} // namespace motovis

#endif //__H__MVP_NODE_READER_H__
